package com.xiaotu.spider.iqy.pageprocessor;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.TVAudienceModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.GsonUtils;
import com.xiaotu.common.util.PageUtils;
import org.apache.commons.lang.StringUtils;
import us.codecraft.webmagic.Page;

import java.util.List;
import java.util.Map;

/**
 * Description
 *
 * @auther GaoHJ
 * @create 2017/11/16
 */
public class PeoplePageProcessor
{
    //人群受众
    private static final String AUDIENCE_URL =
            "https://uaa.if.iqiyi.com/video_index/v2/get_user_profile?album_id=%s";

    //分城贡献
    private static final String CITY_URL =
            "https://uaa.if.iqiyi.com/video_index/v2/get_province_distribution?album_id=%s";


    public static void addPeopleLink(Page page, String dataId)
    {
        PageUtils.addTargetRequest(page, String.format(AUDIENCE_URL, dataId), page.getRequest()
                .getExtras());//人群受众地址
        PageUtils.addTargetRequest(page, String.format(CITY_URL, dataId), page.getRequest()
                .getExtras());//分城贡献地址
    }

    public static Map<String, Object> getDataMap(Page page)
    {
        String json = page.getRawText();
        if (StringUtils.isEmpty(json))
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        Map<String, Object> map = GsonUtils.fromJson(json, Map.class);
        if (map == null || map.isEmpty())
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        return map;
    }

    public static List<Map<String, Object>> getCityData(Map<String, Object> map)
    {
        List<List<Map<String, Object>>> dataList = (List<List<Map<String, Object>>>) map.get
                ("data");
        if (dataList == null || dataList.isEmpty() || dataList.get(0) == null || dataList.get(0)
                .isEmpty())
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        return dataList.get(0);
    }

    public static void peopleProcess(Page page, String tvId)
    {
        Map<String, Object> map = PeoplePageProcessor.getDataMap(page);
        Map<String, Object> dataMap = (Map<String, Object>) map.get("data");
        if (dataMap == null || dataMap.isEmpty())
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        List<Map<String, Object>> details = (List<Map<String, Object>>) dataMap.get("details");
        if (details == null || details.size() < 1)
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        Map<String, Object> detailMap = details.get(0);
        if (detailMap == null || detailMap.isEmpty())
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        Map<String, Object> valueMap = (Map<String, Object>) detailMap.values().iterator().next();

        page.putField(TVAudienceModel.class.getName(), new TVAudienceModel(tvId, null,
                getDataIdFromJson(map), getValueArray("gender", valueMap), getValueArray("age",
                valueMap), getValueArray("education", valueMap), getValueArray("constellation",
                valueMap), getValueArray("interest", valueMap)));
    }

    /**
     * 从json数据中获取dataId
     *
     * @param map
     * @return
     */
    public static String getDataIdFromJson(Map<String, Object> map)
    {
        List<Number> idList = (List<Number>) map.get("ids");
        if (idList == null || idList.isEmpty())
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        return idList.get(0).longValue() + StringUtils.EMPTY;
    }

    /**
     * 获取各个维度的受众数据
     *
     * @param key
     * @param valueMap
     * @return
     */
    private static String[] getValueArray(String key, Map<String, Object> valueMap)
    {
        List<Double> list = (List<Double>) valueMap.get(key);
        if (list == null || list.isEmpty())
            return null;
        return list.stream().map(val -> val.toString()).toArray(String[]::new);
    }
}
